package com.dshore.messagecenter.controller.basedata;


import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dshore.messagecenter.log.LogAnnotation;
import com.dshore.messagecenter.model.basedata.MdApp;
import com.dshore.messagecenter.model.system.ResponseResult;
import com.dshore.messagecenter.service.basedata.MdAppService;


/**
 * 应用管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/application")
public class MdAppController {
	@Autowired
	private MdAppService mcapplyService;
		/**
		 * 分页
		 * @param mcApply
		 * @return
		 */
		@RequestMapping(value="/getPage", method = RequestMethod.POST)
		@ResponseBody
		public Object getPage(@RequestBody MdApp mcApply) {
			try{
				String str ="^[A-Za-z0-9\u4e00-\u9fa5]+$";
				String num ="^[A-Za-z0-9]+$";
				if((Pattern.compile(str).matcher(mcApply.getAppName()).
						find()||StringUtils.isEmpty(mcApply.getAppName().trim()))&&
						(Pattern.compile(num).matcher(mcApply.getAppCode()).find()||
								StringUtils.isEmpty(mcApply.getAppCode().trim()))){
					if(mcApply.getLimit()==null) {
						return "false";
				}else {
					return mcapplyService.queryPageByParam(mcApply);
				}
			}else {
				return "false";
			}
			}catch(Exception e) {
				return null;
			}
		}

	  /**新增时获取校验编码
	   * 
	   * @return
	   */
	  @RequestMapping(value = "/checkCode",method = RequestMethod.POST)
	  @ResponseBody
	  public Object checkCode() {
	  	try {
	  		return mcapplyService.checkCode();
	  	}catch(Exception e) {
			return null;
		}
	  }
	  /**新增
	   * @param mcApply
	   * @return
	   */
	  @LogAnnotation(methodType="新增",targetType="应用",argsName="appName")
	  @RequestMapping(value = "/AugmentApplication",method = RequestMethod.POST)
	  @ResponseBody
	  public ResponseResult<Void> add(MdApp mcApply) {
		ResponseResult<Void>responseResult=new ResponseResult<>(400, "失败");
	  try {	
		  if(check(mcApply)!=true) {
	  			return  responseResult;
	  	}else {
	  		return mcapplyService.save(mcApply);
	  	}
	  }catch(Exception e) {
		  return  responseResult;
		}
	 }	
	  /**
	   * 修改前根据id查询数据
	   * @param id
	   * @return
	   */
	  @RequestMapping( value = "/getById",method = RequestMethod.POST)
	  @ResponseBody
	  public ResponseResult<MdApp> getById(String id) {
		  ResponseResult<MdApp>responseResult=new ResponseResult<>(400, "失败");
		try {
				if(id.length()>32) {
				  return responseResult;
			  }else {
				  return mcapplyService.getById(id);
			  }
		}catch(Exception e) {
			  return  responseResult;
			}
	  }
	  /**
	   * 保存修改
	   * @param mcApply
	   * @return
	   */
	  @LogAnnotation(methodType="修改",targetType="应用",argsName="appName")
	  @RequestMapping( value = "/save",method = RequestMethod.POST)
	  @ResponseBody
	  public ResponseResult<Void> save(MdApp mcApply) {
		  ResponseResult<Void>responseResult=new ResponseResult<>(400, "失败");
	  	try{
	  		if(check(mcApply)!=true) {
	  			return  responseResult;
		  	}else if(StringUtils.isNotBlank(mcApply.getId())){
		  		return  responseResult;
		  	}else {
		  		return mcapplyService.midify(mcApply);
		  	}
	  	}catch(Exception e) {
			  return  responseResult;
			}
	  }
		/**
		 * 密钥重置
		 * @param id
		 * @return
		 */
		@LogAnnotation(methodType="更新",targetType="应用密钥",tableName="T_MD_APP",argsName="id",fieldName="APP_NAME")
		@RequestMapping(value = "/replacement",method = RequestMethod.POST)
		@ResponseBody
		public ResponseResult<Void> keyreset( String id) {
			 ResponseResult<Void>responseResult=new ResponseResult<>(400, "失败");
			 try{
				 return mcapplyService.keyreset(id);
			 }catch(Exception e) {
				  return  responseResult;
				}
		}
		/**
		 * 根据id下载密钥
		 */
		@RequestMapping("/queuekey")
		@ResponseBody
		public void queuenetkey( String id,HttpServletResponse response) {
				mcapplyService.queuekey(id,response);
		}
		/**
		 * 根据id下载服务密钥
		 * @param id
		 * @return
		 */
		@RequestMapping("/servicenetkey")
		@ResponseBody
		public void servicenetkey(String id,HttpServletResponse response) {
			 mcapplyService.servicekey(id,response);
		}
		/**
		 * 批量删除
		 * @param id
		 * @return
		 */
		@LogAnnotation(methodType="删除",targetType="应用",tableName="T_MD_APP",argsName="id",fieldName="APP_NAME")
		@RequestMapping(value = "/deleteAll",method = RequestMethod.POST)
		@ResponseBody
		public ResponseResult<Void> deleteAll( String id) {
				 ResponseResult<Void>responseResult=new ResponseResult<>(400, "失败");
				 try{
					 return mcapplyService.deleteAll(id);
				 }catch(Exception e) {
					  return  responseResult;
					}
		}
		/**
		 * 
		 * 查询应用分类（三集五大）
		 * @return
		 */
		@RequestMapping(value = "/selectByAppType",method = RequestMethod.POST)
		@ResponseBody
		public Object selectByAppType() {
			return mcapplyService.selectByAppType();
		}
		/**
		 * 查询网络状态（外网/内网）
		 * @return
		 */
		@RequestMapping("/selectByInternetType")
		@ResponseBody
		public Object selectByInternetType() {
			return mcapplyService.selectByInternetType();
		}
	/**
	 * 新增时验证应用名称是否重复
	 * @param appName
	 * @return
	 */
	@RequestMapping(value = "/checkName",method = RequestMethod.POST)
	@ResponseBody
	public String checkName( String appName) {
		if(Pattern.compile("^[a-zA-Z0-9\\u4e00-\\u9fa5]{1,32}+$").matcher(appName).find()) {
			return mcapplyService.checkName(appName,"");
		}else {
			return "false";
		}
	}
	/**
	 * 修改时验证应用名称是否重复
	 * @param appName
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/checkAppName",method = RequestMethod.POST)
	@ResponseBody
	public String checkAppName(String appName,String id) {
		if(Pattern.compile("^[a-zA-Z0-9\\u4e00-\\u9fa5]{1,32}+$").matcher(appName).find()) {
			return mcapplyService.checkName(appName,id);
		}else {
			return "false";
		}
	}	
	/**
	 * 树状图
	 * @return
	 */
	@RequestMapping(value = "/gettree",method = RequestMethod.POST)
	@ResponseBody
	public String gettree() {
		return mcapplyService.gettree();
	}
	//校验
	private  boolean check(MdApp mcApply) {
		if(StringUtils.isBlank(mcApply.getAppName())||
		  			StringUtils.isBlank(mcApply.getAppCode())||
		  				StringUtils.isBlank(mcApply.getAppType())||
		  					StringUtils.isBlank(mcApply.getPrincipal())||
		  						StringUtils.isBlank(mcApply.getPrincipalContact())) {
				return  false;
		  	}else if(mcApply.getAppName().length()>32||mcApply.getAppType().length()!=4||
		  			 mcApply.getPrincipal().length()>32||mcApply.getPrincipalContact().length()>11||
		  			 mcApply.getAppCode().length()>32){
		  		return  false;
		  	}else if(!Pattern.compile("^[a-zA-Z0-9\\u4e00-\\u9fa5]+$").matcher(mcApply.getAppName()).find()||
		  		   	!Pattern.compile("^[a-zA-Z0-9\\u4e00-\\u9fa5]+$").matcher(mcApply.getPrincipal()).find()||
		  		   	!Pattern.compile("^((0\\d{10})|(1[3584]\\d{9}))$").matcher(mcApply.
		  		   	getPrincipalContact()).find()||!Pattern.compile("^(App[0-9]{6})$").matcher(mcApply.
				  	getAppCode()).find()){
		  		return  false;
		  	}else if(!mcApply.getAppType().equals("1101")&&!mcApply.getAppType().equals("1102")&&
		   			 !mcApply.getAppType().equals("1103")&&!mcApply.getAppType().equals("1104")&&
		   			 !mcApply.getAppType().equals("1105")&&!mcApply.getAppType().equals("1106")&&
		   			 !mcApply.getAppType().equals("1107")&&!mcApply.getAppType().equals("1108")&&
		   			 !mcApply.getAppType().equals("1109")) {
		  		return  false;
		  	}else if(!mcApply.getInternetType().equals("1201")&&!mcApply.getInternetType().equals("1202")){
		  		return  false;
		  	}else {
		  		return  true;
		  	}
			
	}
}
